package com.artron.mediaartron.ui.fragment.center;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.baselib.entity.Response;
import com.artron.baselib.utils.ToastUtil;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.config.AppProfile;
import com.artron.mediaartron.data.entity.User;
import com.artron.mediaartron.data.net.RetrofitHelper;
import com.artron.mediaartron.data.net.api.CouponApi;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.functions.Action1;

/**
 * 优惠券
 * Created by qi on 2017/12/11.
 */

public class CouponFragment extends BaseStaticFragment {

    @BindView(R.id.fragment_coupon_et_coupon)
    EditText mEtCoupon;
    @BindView(R.id.fragment_coupon_btn_coupon)
    Button mBtnConvert;
    @BindView(R.id.fragment_coupon_RecyclerView)
    RecyclerView mRecyclerView;
    Unbinder unbinder;

    public static CouponFragment newInstance() {
        CouponFragment fragment = new CouponFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_coupon;
    }

    @Override
    protected void initView() {

    }


    @OnClick(R.id.fragment_coupon_btn_coupon)
    public void onViewClicked() {
        String coupon = mEtCoupon.getText().toString().trim();
        User user = AppProfile.getUserInfo().getUser();
        if (!TextUtils.isEmpty(coupon)) {
            if (user != null) {
                CouponApi couponApi = RetrofitHelper.getCouponApi();
                RetrofitHelper.subscript(couponApi.assignCoupon(coupon, user.getPassId()), new Action1<Response>() {
                    @Override
                    public void call(Response response) {
                        if (response.isStatus()) {
                            ToastUtil.show("兑换成功");
                            mEtCoupon.setText("");
                        } else {
                            ToastUtil.show(response.getMessage());
                        }
                    }
                });
            }
        } else {
            ToastUtil.show("优惠码不能为空");
        }
    }
}
